package com.cdshangpai.utils.offlinecache;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
//import android.net.NetworkInfo;
import android.util.Log;

import com.cdshangpai.PhotoappApplication;
import com.cdshangpai.photoapp.LogConfig;

public class NetworkState {

    /**
     * 检测网络是否链接
     * @return
     */
    public boolean isconnect() {
        Context context= PhotoappApplication.context;
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            Network network =mConnectivityManager.getActiveNetwork();
            if(network==null)
            {
                return false;
            }else
            {
                NetworkCapabilities networkCapabilities = mConnectivityManager.getNetworkCapabilities(network);
                if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET))
                {
                    return true;
                }
            }
            /*NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
            if(networkInfo!=null&&networkInfo.isAvailable())
            {
                return true;
            }*/
        }
        return false;
    }

    //是否联网
    public boolean isOnline()
    {
        Boolean isconnect=isconnect();

        Log.i(LogConfig.logTag,"网络链接状态:"+isconnect);

        return isconnect;
    }

    //获取当前网络连接的类型信息
    public static int getConnectedType() {
        /*Context context= PhotoappApplication.context;
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
                return mNetworkInfo.getType();
            }
        }*/
        return -1;
    }

    //网络状态变化接收器
    BroadcastReceiver connectionReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);

            Network network =connectMgr.getActiveNetwork();
            if(network==null)
            {
                Log.w(LogConfig.logTag,"network closeed!");
            }else
            {
                NetworkCapabilities networkCapabilities = connectMgr.getNetworkCapabilities(network);
                if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET))
                {
                    Log.w(LogConfig.logTag,"network opened!");
                    //网络打开后，通知离线表单做一次提交
                    FormSubmit formSubmit=new FormSubmit();
                    formSubmit.notifySubmit();
                }
                else
                {
                    Log.w(LogConfig.logTag,"network closeed!");
                }
            }


            /*NetworkInfo netInfo=connectMgr.getActiveNetworkInfo();
            if(netInfo==null)
            {
                return;
            }
            if (!netInfo.isConnected()) {
                //网络关闭
                Log.w(LogConfig.logTag,"network closeed!");
            }else {
                //网络打开
                Log.w(LogConfig.logTag,"network opened!");
                //网络打开后，通知离线表单做一次提交
                FormSubmit formSubmit=new FormSubmit();
                formSubmit.notifySubmit();
            }*/
        }
    };

    //注册网络状态变化监听
    public void registNetworkStateListener()
    {
        Context context=PhotoappApplication.context;
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(connectionReceiver, intentFilter);
    }

    //注销网络状态变化监听
    public void unregistNetworkStateListener()
    {
        Context context=PhotoappApplication.context;
        if (connectionReceiver != null) {
            context.unregisterReceiver(connectionReceiver);
        }
    }
}
